package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.po.keyhistory.KeyHistoryPo;
import com.bwda.dsrs.filecenter.domain.condition.keyhistory.KeyHistoryCondition;
import com.bwda.dsrs.filecenter.domain.form.keyhistory.KeyHistoryQueryForm;
import com.bwda.dsrs.filecenter.domain.form.keyhistory.KeyHistoryUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.keyhistory.KeyHistoryCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.keyhistory.KeyHistoryVo;
import com.bwda.dsrs.filecenter.service.KeyHistoryService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/keyhistory")
@Api(value = "/b/keyhistory", tags = "秘钥版本历史")
public class KeyHistoryController extends BaseController {

	@Autowired
	private KeyHistoryService keyHistoryService;

	@ApiOperation(value = "通过应用系统编码查询秘钥版本历史",notes = "通过应用系统编码查询秘钥版本历史",httpMethod = "GET")
	@RequestMapping(value = "/queryListByAppCode", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<KeyHistoryVo>> queryListByAppCode(@ApiParam(value = "应用编码code", required = true)@RequestParam String appCode) throws BusinessServiceException {
		List<KeyHistoryPo> poList = keyHistoryService.queryKeyHistoryList(appCode);
		List<KeyHistoryVo> voList = CopyUtil.transfer(poList, KeyHistoryVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询秘钥版本历史",notes = "查询秘钥版本历史",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<KeyHistoryVo> query(@ApiParam(value = "历史秘钥id", required = true)@RequestParam Integer keyHistoryId) throws BusinessServiceException {
		KeyHistoryPo po = keyHistoryService.queryWithValid(keyHistoryId);
		KeyHistoryVo vo = CopyUtil.transfer(po, KeyHistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询秘钥版本历史数量",notes = "查询秘钥版本历史数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid KeyHistoryQueryForm form) throws BusinessServiceException {
		KeyHistoryCondition condition = this.getConditionByQueryForm(form);
		int count = keyHistoryService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询秘钥版本历史列表",notes = "查询秘钥版本历史列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<KeyHistoryVo>> queryList(@ModelAttribute@Valid KeyHistoryQueryForm form) throws BusinessServiceException {
		KeyHistoryCondition condition = this.getConditionByQueryForm(form);
		List<KeyHistoryPo> poList = keyHistoryService.queryList(condition);
		List<KeyHistoryVo> voList = CopyUtil.transfer(poList, KeyHistoryVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询秘钥版本历史列表(带分页)",notes = "查询秘钥版本历史列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<KeyHistoryVo>> queryPageList(@ModelAttribute@Valid KeyHistoryQueryForm form) throws BusinessServiceException {
		KeyHistoryCondition condition = this.getConditionByQueryForm(form);
		List<KeyHistoryVo> voList = new ArrayList<>();
		int count = keyHistoryService.queryCount(condition);
		if (count > 0) {
			List<KeyHistoryPo> poList = keyHistoryService.queryList(condition);
			voList = CopyUtil.transfer(poList, KeyHistoryVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增秘钥版本历史",notes = "新增秘钥版本历史",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<KeyHistoryVo> add(@ModelAttribute@Valid KeyHistoryCreateForm form) throws BusinessServiceException {
		KeyHistoryPo po = CopyUtil.transfer(form, KeyHistoryPo.class);
//		po.setKeyHistoryId(RandomUtils.nextInt());
		keyHistoryService.insert(po);
		KeyHistoryVo vo = CopyUtil.transfer(po, KeyHistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改秘钥版本历史",notes = "修改秘钥版本历史",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid KeyHistoryUpdateForm form) throws BusinessServiceException {
		KeyHistoryPo po = CopyUtil.transfer(form, KeyHistoryPo.class);
		keyHistoryService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除秘钥版本历史",notes = "删除秘钥版本历史",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "历史秘钥id", required = true)@RequestParam Integer keyHistoryId) throws BusinessServiceException {
		keyHistoryService.delete(keyHistoryId);
		return getSuccessResult();
	}

	/**
	 * KeyHistoryQueryForm转换为KeyHistoryCondition
	 *
	 * @param form
	 * @return
	 */
	private KeyHistoryCondition getConditionByQueryForm(KeyHistoryQueryForm form) {
		KeyHistoryCondition condition = CopyUtil.transfer(form, KeyHistoryCondition.class);
		return condition;
	}

}